home *** CD-ROM | disk | FTP | other *** search
/ Aminet 49 / Aminet 49 (2002)(GTI - Schatztruhe)[!][Jun 2002].iso / Aminet / util / libs / ttrender.lha / ttrender-2.0 / Developer / source / smooth / ftsmooth.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-06  |  6.1 KB  |  220 lines

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /*  ftsmooth.c                                                             */
  4. /*                                                                         */
  5. /*    Anti-aliasing renderer interface (body).                             */
  6. /*                                                                         */
  7. /*  Copyright 2000-2001 by                                                 */
  8. /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
  9. /*                                                                         */
  10. /*  This file is part of the FreeType project, and may only be used,       */
  11. /*  modified, and distributed under the terms of the FreeType project      */
  12. /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
  13. /*  this file you indicate that you have read the license and              */
  14. /*  understand and accept it fully.                                        */
  15. /*                                                                         */
  16. /***************************************************************************/
  17.  
  18.  
  19. #include <ft2build.h>
  20. #include FT_INTERNAL_OBJECTS_H
  21. #include FT_OUTLINE_H
  22. #include "ftsmooth.h"
  23. #include "ftgrays.h"
  24.  
  25. #include "ftsmerrs.h"
  26.  
  27.  
  28.   /* initialize renderer -- init its raster */
  29.   static FT_Error
  30.   ft_smooth_init( FT_Renderer  render )
  31.   {
  32.     FT_Library  library = FT_MODULE_LIBRARY( render );
  33.  
  34.  
  35.     render->clazz->raster_class->raster_reset( render->raster,
  36.                                                library->raster_pool,
  37.                                                library->raster_pool_size );
  38.  
  39.     return 0;
  40.   }
  41.  
  42.  
  43.   /* sets render-specific mode */
  44.   static FT_Error
  45.   ft_smooth_set_mode( FT_Renderer  render,
  46.                       FT_ULong     mode_tag,
  47.                       FT_Pointer   data )
  48.   {
  49.     /* we simply pass it to the raster */
  50.     return render->clazz->raster_class->raster_set_mode( render->raster,
  51.                                                          mode_tag,
  52.                                                          data );
  53.   }
  54.  
  55.   /* transform a given glyph image */
  56.   static FT_Error
  57.   ft_smooth_transform( FT_Renderer   render,
  58.                        FT_GlyphSlot  slot,
  59.                        FT_Matrix*    matrix,
  60.                        FT_Vector*    delta )
  61.   {
  62.     FT_Error  error = Smooth_Err_Ok;
  63.  
  64.  
  65.     if ( slot->format != render->glyph_format )
  66.     {
  67.       error = Smooth_Err_Invalid_Argument;
  68.       goto Exit;
  69.     }
  70.  
  71.     if ( matrix )
  72.       FT_Outline_Transform( &slot->outline, matrix );
  73.  
  74.     if ( delta )
  75.       FT_Outline_Translate( &slot->outline, delta->x, delta->y );
  76.  
  77.   Exit:
  78.     return error;
  79.   }
  80.  
  81.  
  82.   /* return the glyph's control box */
  83.   static void
  84.   ft_smooth_get_cbox( FT_Renderer   render,
  85.                       FT_GlyphSlot  slot,
  86.                       FT_BBox*      cbox )
  87.   {
  88.     MEM_Set( cbox, 0, sizeof ( *cbox ) );
  89.  
  90.     if ( slot->format == render->glyph_format )
  91.       FT_Outline_Get_CBox( &slot->outline, cbox );
  92.   }
  93.  
  94.  
  95.   /* convert a slot's glyph image into a bitmap */
  96.   static FT_Error
  97.   ft_smooth_render( FT_Renderer   render,
  98.                     FT_GlyphSlot  slot,
  99.                     FT_UInt       mode,
  100.                     FT_Vector*    origin )
  101.   {
  102.     FT_Error     error;
  103.     FT_Outline*  outline = NULL;
  104.     FT_BBox      cbox;
  105.     FT_UInt      width, height, pitch;
  106.     FT_Bitmap*   bitmap;
  107.     FT_Memory    memory;
  108.  
  109.     FT_Raster_Params  params;
  110.  
  111.  
  112.     /* check glyph image format */
  113.     if ( slot->format != render->glyph_format )
  114.     {
  115.       error = Smooth_Err_Invalid_Argument;
  116.       goto Exit;
  117.     }
  118.  
  119.     /* check mode */
  120.     if ( mode != ft_render_mode_normal )
  121.       return Smooth_Err_Cannot_Render_Glyph;
  122.  
  123.     outline = &slot->outline;
  124.  
  125.     /* translate the outline to the new origin if needed */
  126.     if ( origin )
  127.       FT_Outline_Translate( outline, origin->x, origin->y );
  128.  
  129.     /* compute the control box, and grid fit it */
  130.     FT_Outline_Get_CBox( outline, &cbox );
  131.  
  132.     cbox.xMin &= -64;
  133.     cbox.yMin &= -64;
  134.     cbox.xMax  = ( cbox.xMax + 63 ) & -64;
  135.     cbox.yMax  = ( cbox.yMax + 63 ) & -64;
  136.  
  137.     width  = ( cbox.xMax - cbox.xMin ) >> 6;
  138.     height = ( cbox.yMax - cbox.yMin ) >> 6;
  139.     bitmap = &slot->bitmap;
  140.     memory = render->root.memory;
  141.  
  142.     /* release old bitmap buffer */
  143.     if ( slot->flags & ft_glyph_own_bitmap )
  144.     {
  145.       FREE( bitmap->buffer );
  146.       slot->flags &= ~ft_glyph_own_bitmap;
  147.     }
  148.  
  149.     /* allocate new one, depends on pixel format */
  150.     pitch = width;
  151.     bitmap->pixel_mode = ft_pixel_mode_grays;
  152.     bitmap->num_grays  = 256;
  153.     bitmap->width      = width;
  154.     bitmap->rows       = height;
  155.     bitmap->pitch      = pitch;
  156.  
  157.     if ( ALLOC( bitmap->buffer, (FT_ULong)pitch * height ) )
  158.       goto Exit;
  159.  
  160.     slot->flags |= ft_glyph_own_bitmap;
  161.  
  162.     /* translate outline to render it into the bitmap */
  163.     FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin );
  164.  
  165.     /* set up parameters */
  166.     params.target = bitmap;
  167.     params.source = outline;
  168.     params.flags  = ft_raster_flag_aa;
  169.  
  170.     /* render outline into the bitmap */
  171.     error = render->raster_render( render->raster, ¶ms );
  172.     
  173.     FT_Outline_Translate( outline, cbox.xMin, cbox.yMin );
  174.  
  175.     if ( error )
  176.       goto Exit;
  177.  
  178.     slot->format      = ft_glyph_format_bitmap;
  179.     slot->bitmap_left = cbox.xMin >> 6;
  180.     slot->bitmap_top  = cbox.yMax >> 6;
  181.  
  182.   Exit:
  183.     if ( outline && origin )
  184.       FT_Outline_Translate( outline, -origin->x, -origin->y );
  185.  
  186.     return error;
  187.   }
  188.  
  189.  
  190.   FT_CALLBACK_TABLE_DEF
  191.   const FT_Renderer_Class  ft_smooth_renderer_class =
  192.   {
  193.     {
  194.       ft_module_renderer,
  195.       sizeof( FT_RendererRec ),
  196.  
  197.       "smooth",
  198.       0x10000L,
  199.       0x20000L,
  200.  
  201.       0,    /* module specific interface */
  202.  
  203.       (FT_Module_Constructor)ft_smooth_init,
  204.       (FT_Module_Destructor) 0,
  205.       (FT_Module_Requester)  0
  206.     },
  207.  
  208.     ft_glyph_format_outline,
  209.  
  210.     (FTRenderer_render)   ft_smooth_render,
  211.     (FTRenderer_transform)ft_smooth_transform,
  212.     (FTRenderer_getCBox)  ft_smooth_get_cbox,
  213.     (FTRenderer_setMode)  ft_smooth_set_mode,
  214.  
  215.     (FT_Raster_Funcs*)    &ft_grays_raster
  216.   };
  217.  
  218.  
  219. /* END */
  220.